﻿@using Microsoft.AspNetCore.Html
@using WB.UI.Designer.BootstrapSupport
@using WB.UI.Designer.BootstrapSupport.HtmlHelpers
@using WB.UI.Designer.Code
@using WB.UI.Designer.Extensions
@using WB.UI.Designer.Utils
@using WB.UI.Shared.Web.Extensions
@model IPagedList<QuestionnaireListViewModel>

<div class="wrapper-questionnaire-table clearfix">
    <table id="questionnaire-table-header" class="table table-header @ViewBag.TableCssClass">
        <thead>
            <tr>
                @foreach (var field in new[] {new {ColumnName = "Title", Title = QuestionnaireController.Title},
                new {ColumnName = "LastEntryDate", Title = QuestionnaireController.LastModified},
                new {ColumnName = "CreationDate", Title = QuestionnaireController.CreatedDate},
                new {ColumnName = "CreatorName", Title = QuestionnaireController.CreatedBy}})
                {
                    var isTableSortedByThisField = ViewBag.SortBy == field.ColumnName;
                    <th class="@(ViewBag.SortBy == field.ColumnName ? "ordered" : "")">
                        <a href="@ActionWithOrderField(field.ColumnName)">
                            @field.Title
                            @{
                                if (isTableSortedByThisField)
                                {
                                    <span class="glyphicon glyphicon-triangle-@(((int?) ViewBag.SortOrder).ToBool() ? "bottom" : "top")"></span>
                                }
                            }
                        </a>
                        @if (field.ColumnName == "Title")
                        {
                            <partial name="_list_search_form"/>
                        }
                    </th>
                }
                <th>
                </th>
            </tr>
        </thead>
    </table>
    <div id="table-content-holder" class="list-with-icons @ViewBag.TableCssClass">
        <div class="scroller-container">
            @if (!string.IsNullOrWhiteSpace(ViewData["UserNotification"] as string)
          || TempData.ContainsKey(Alerts.SUCCESS)
          || TempData.ContainsKey(Alerts.ERROR))
            {
                <div id="table-message">
                    <p>
                        @if (TempData.ContainsKey(Alerts.SUCCESS))
                        {
                            @(Html.Raw(@TempData[Alerts.SUCCESS]))
                        }
                        else if (TempData.ContainsKey(Alerts.ERROR))
                        {
                            @(Html.Raw(@TempData[Alerts.ERROR]))
                        }
                        else
                        {
                            @(Html.Raw(ViewData["UserNotification"]))
                        }
                    </p>
                    <a href="#" class="cross" title="@QuestionnaireController.Close" onclick=" $('#table-message').hide() "></a>
                </div>
            }

            @if (Model!.TotalCount == 0)
            {
                <p class="text-center" style="line-height: 10em">@QuestionnaireController.EmptyList</p>
            }
            else
            {
                <table id="questionnaire-table-content" class="table table-content ">
                    <tbody>
                        @foreach (var listViewModel in Model)
                        {
                            var routevalues = listViewModel.GetIdValue();
                            var itemId = listViewModel.Id;
                            var itemType = listViewModel.GetLabel();
                            var itemName = Uri.EscapeDataString(listViewModel.GetName());
                            var pdfDownloadUrl = Url.Action("downloadPdf", "Pdf", routevalues.AddArea("Pdf"));
                            var htmlDownloadUrl = Url.Action("PrintPreview", "Pdf", routevalues.AddArea("Pdf"));
                            var pdfStatusUrl = Url.Action("statusPdf", "Pdf", routevalues.AddArea("Pdf"));
                            var pdfRetryUrl = Url.Action("Retry", "Pdf", routevalues.AddArea("Pdf"));
                            var getLanguagesUrl = Url.Action("GetLanguages", "Questionnaire", routevalues);
                            var openActionName = listViewModel.IsFolder ? "Public" : "Details";
                            var iconCssClass = listViewModel.IsFolder ? "tree-icon folder-closed" : "tree-icon";

                            <tr class="@(listViewModel.IsDeleted ? "deleted" : "")">
                                <td class="search-value">
                                    <a href="@Url.Action(openActionName, listViewModel.IsFolder ? "Questionnaire" : "Q", routevalues)" title="@listViewModel.Location">
                                        <span class="@iconCssClass"></span>
                                        @listViewModel.Title
                                    </a>
                                </td>
                                <td class="date">
                                    <time datetime="@listViewModel.LastEntryDate.ToString("o")">@listViewModel.LastEntryDate.ConvertToLocalAndFormatDate()</time>

                                </td>
                                <td class="date">
                                    <time datetime="@listViewModel.CreationDate.ToString("o")">@listViewModel.CreationDate.ConvertToLocalAndFormatDate()</time>
                                </td>
                                <td class="email">
                                    @listViewModel.CreatedBy
                                </td>
                                <td>
                                    @{
                                        var actionModel = listViewModel as IActionItem;
                                        if (actionModel != null)
                                        {
                                            if (actionModel.CanCopy || actionModel.CanDelete || actionModel.CanExport || actionModel.CanSynchronize || actionModel.CanOpen)
                                            {
                                                <div class="btn-group pull-right">
                                                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
                                                        @QuestionnaireController.Action
                                                        <span class="caret"></span>
                                                    </a>
                                                    <ul class="dropdown-menu">
                                                        <li>@Html.ActionLink(QuestionnaireController.Open, openActionName,listViewModel.IsFolder? "Questionnaire" : "Q", routevalues, null)</li>
                                                        @{
                                                            if (actionModel.CanCopy)
                                                            {
                                                                <li>@Html.ActionLink(QuestionnaireController.Copy, "Clone", "Questionnaire", routevalues, null)</li>
                                                            }
                                                            if (actionModel.CanExportToPdf)
                                                            {
                                                                <li class="divider"></li>
                                                                <li>
                                                                    <a href="#mExportPdf" onclick='questionnaireActionModel.exportItemAsPdf("@itemId", "@itemType", "@itemName", "@pdfDownloadUrl", "@pdfStatusUrl", "@pdfRetryUrl", "@getLanguagesUrl")' data-toggle="modal">@QuestionnaireController.ExportAsPDF</a>
                                                                </li>
                                                            }
                                                            if (actionModel.CanExportToHtml)
                                                            {
                                                                <li>
                                                                    <a onclick='questionnaireActionModel.exportItemAsHtml("@itemId", "@itemType", "@itemName", "@htmlDownloadUrl", "@getLanguagesUrl")'>@QuestionnaireController.ExportAsHtml</a>
                                                                </li>
                                                            }

                                                            @*if (actionModel.CanSynchronize)
                                                            {
                                                                <li>@Html.ActionLink(QuestionnaireController.Backup, "Backup", "Questionnaire", routevalues, null)</li>
                                                            }*@
                                                            if (actionModel.CanAssignFolder)
                                                            {
                                                                <li class="divider"></li>
                                                                <li><a href="#mAssignFolder" onclick='questionnaireActionModel.assignFolder("@itemId", "@itemType", "@itemName")' data-toggle="modal">@QuestionnaireController.AssignFolder</a></li>
                                                            }
                                                            if (actionModel.CanDelete)
                                                            {
                                                                <li class="divider"></li>
                                                                <li><a href="#mDelete" onclick='questionnaireActionModel.deleteItem("@itemId", "@itemType", "@itemName")' data-toggle="modal">@QuestionnaireController.Delete</a></li>
                                                            }
                                                        }
                                                    </ul>
                                                </div>
                                            }
                                        }
                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                @Html.Pager(Model.PageIndex + 1, Model.TotalPages,
                    x => Url.Action(ViewContext.CurrentAction(), ViewContext.CurrentController(), new { p = x, sb = ViewBag.SortBy, so = ViewBag.SortOrder, f = ViewBag.Filter }), 2)
            }
        </div>
    </div>
</div>

@functions
{
    private string? ActionWithOrderField(string sortField)
    {
        var sortOrder = (int?)ViewBag.SortOrder;
        return Url.Action(
            ViewContext.CurrentAction(),
            new
            {
                p = ViewBag.PageIndex,
                sb = sortField,
                so = CustomExtensions.InvertBooleableInt(sortOrder, ViewBag.SortBy == sortField),
                f = ViewBag.Filter,
                id = ViewBag.FolderId
            });
    }
}
